function [sys,x0,str,ts,simStateCompliance] = SetState(t,x,u,flag)

switch flag,

  %%%%%%%%%%%%%%%%%%
  % Initialization %
  %%%%%%%%%%%%%%%%%%
  case 0,
    [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;

  %%%%%%%%%%%%%%%
  % Derivatives %
  %%%%%%%%%%%%%%%
  case 1,
    sys=mdlDerivatives(t,x,u);

  %%%%%%%%%%
  % Update %
  %%%%%%%%%%
  case 2,
    sys=mdlUpdate(t,x,u);

  %%%%%%%%%%%
  % Outputs %
  %%%%%%%%%%%
  case 3,
    sys=mdlOutputs(t,x,u);

  %%%%%%%%%%%%%%%%%%%%%%%
  % GetTimeOfNextVarHit %
  %%%%%%%%%%%%%%%%%%%%%%%
  case 4,
    sys=mdlGetTimeOfNextVarHit(t,x,u);

  %%%%%%%%%%%%%
  % Terminate %
  %%%%%%%%%%%%%
  case 9,
    sys=mdlTerminate(t,x,u);

  %%%%%%%%%%%%%%%%%%%%
  % Unexpected flags %
  %%%%%%%%%%%%%%%%%%%%
  otherwise
    DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));

end

% end sfuntmpl

%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes

%
% call simsizes for a sizes structure, fill it in and convert it to a
% sizes array.
%
% Note that in this example, the values are hard coded.  This is not a
% recommended practice as the characteristics of the block are typically
% defined by the S-function parameters.
%
sizes = simsizes;

sizes.NumContStates  = 0;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 0;
sizes.NumInputs      = 0;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;   % at least one sample time is needed

sys = simsizes(sizes);

%
% initialize the initial conditions
%
x0  = [];

%
% str is always an empty matrix
%
str = [];

%
% initialize the array of sample times
%
ts  = [0 0];

% Specify the block simStateCompliance. The allowed values are:
%    'UnknownSimState', < The default setting; warn and assume DefaultSimState
%    'DefaultSimState', < Same sim state as a built-in block
%    'HasNoSimState',   < No sim state
%    'DisallowSimState' < Error out when saving or restoring the model sim state
simStateCompliance = 'UnknownSimState';

% end mdlInitializeSizes

%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function sys=mdlDerivatives(t,x,u)

sys = [];

% end mdlDerivatives

%
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================
%
function sys=mdlUpdate(t,x,u)

sys = [];

% end mdlUpdate

%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(t,x,u)
global flag1 flag2  flag3 flag4 flag5 flag6  times gridstates outsState
global to2 to3 to4 to5  to6 time
if t == 0
res = load('times.mat');
res1 = load('GridStates.mat');
res2 = load('OutState.mat');
times = res.times; gridstates = res1.Grid;outsState = res2.Outs;
end
Stoptime = str2num(get_param('Tester','StopTime'));
if t < times(1)
% 设置状态1
    if flag1 == false
        param = gridstates(:,1);
        param = [param;outsState(:,1)];
        mysetparam(param);
        flag1 = true;
    end
    if to2 == true
        param = gridstates(:,2);
        param = [param;outsState(:,2)];
        mysetparam(param);
        flag2 = true;
    end
    if Stoptime < times(2)
        if to3 == true
            param = gridstates(:,3);
            param = [param;outsState(:,3)];
            mysetparam(param);
            flag3 = true;
    end
    end
else if t < times(2)
%设置状态2
    if flag2 == false
        param = gridstates(:,2);
        param = [param;outsState(:,2)];
        mysetparam(param);
        flag2 = true;
    end
    if to3 == true
        param = gridstates(:,3);
        param = [param;outsState(:,3)];
        mysetparam(param);
        flag3 = true;
    end
%设置状态3
else if t < times(3)
      if flag3 == false
        param = gridstates(:,3);
        param = [param;outsState(:,3)];
        mysetparam(param);
        flag3 = true;
      end  
%设置状态4
else if t < times(4)
      if flag4 == false
        param = gridstates(:,4);
        param = [param;outsState(:,4)];
        mysetparam(param);
        flag4 = true;
      end  
%设置状态5
else if t < times(5)
      if flag5 == false
        param = gridstates(:,5);
        param = [param;outsState(:,5)];
        mysetparam(param);
        flag5 = true;
      end 
%设置状态6
else if t < times(6)
       if flag6 == false
        param = gridstates(:,6);
        param = [param;outsState(:,6)];
        mysetparam(param);
        flag6 = true;
       end 
%否则，不进行任何操作
else
       if flag6 == false
        param = gridstates(:,6);
        param = [param;outsState(:,6)];
        mysetparam(param);
        flag6 = true;
       end 
end
end
end
end
end
end
sys = [];

% end mdlOutputs

%
%=============================================================================
% mdlGetTimeOfNextVarHit
% Return the time of the next hit for this block.  Note that the result is
% absolute time.  Note that this function is only used when you specify a
% variable discrete-time sample time [-2 0] in the sample time array in
% mdlInitializeSizes.
%=============================================================================
%
function sys=mdlGetTimeOfNextVarHit(t,x,u)

sampleTime = 1;    %  Example, set the next hit to be one second later.
sys = t + sampleTime;

% end mdlGetTimeOfNextVarHit

%
%=============================================================================
% mdlTerminate
% Perform any end of simulation tasks.
%=============================================================================
%
function sys=mdlTerminate(t,x,u)

sys = [];

% end mdlTerminate
